package dao;

import pojo.Major;
import utils.JDBCUtils;

import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author leiHua
 * @Date 2022/10/28 15:47
 */
public class MajorDao {
    public static List<Major> findAllMajors() throws Exception {
        Connection conn = JDBCUtils.getconnection();
        //2.3 创建预处理命令对象
        String sql = "select * from tb_major";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();
        Major major = null;
        List<Major> majorList = new ArrayList<>();
        while (resultSet.next()) {
            major = new Major();
            major.setId(resultSet.getString("id"));
            major.setMajorName(resultSet.getString("major_name"));
            majorList.add(major);
        }
        ps.close();
        conn.close();
        return majorList;
    }

    public static int addMajor(Major major) {
        //2. 信息处理：使用JDBC添加专业信息
        try {
            Connection conn = JDBCUtils.getconnection();
            //2.3 创建预处理命令对象
            String sql = "insert into tb_major(id,major_name) values(?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            //2.4 执行SQL语句
            ps.setString(1, major.getId());
            ps.setString(2, major.getMajorName());
            int rows = ps.executeUpdate();
            //2.5 释放资源
            ps.close();
            conn.close();
            return rows;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static Major findMajorById(String id) {

        Major major = new Major();
        try {

            Connection conn = JDBCUtils.getconnection();
            //1.3 创建预处理命令对象
            String sql = "SELECT * FROM tb_major where id='" + id + "'";
            PreparedStatement ps = conn.prepareStatement(sql);
            //1.4 执行SQL语句
            //ps.setString(1,id);
            ResultSet rs = ps.executeQuery(sql);
            if (rs.next()) {
                major.setId(rs.getString("id"));
                major.setMajorName(rs.getString("major_name"));
            }
            //1.5 释放资源
            rs.close();
            ps.close();
            conn.close();
            return major;
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(major.getMajorName());
        return null;
    }

    public static  int updateMajor(Major major){
        //1. 收集数据

        String id=major.getId();
        String majorName=major.getMajorName();
        //2. 信息处理：使用JDBC修改专业信息
        try {

            Connection conn = JDBCUtils.getconnection();
            //2.3 创建预处理命令对象
            String sql="update tb_major set major_name=? where id=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            //2.4 执行SQL语句
            ps.setString(2,id);
            ps.setString(1,majorName);
            int rows=ps.executeUpdate();
            //2.5 释放资源
            ps.close();	conn.close();
            return rows;
        }catch (Exception e){
            e.printStackTrace();
        }
       return 0;
    }

    public static int  deleteMajorById(String id){
//1. 收集数据
        //2. 信息处理：使用JDBC删除指定ID的专业信息
        try {

            Connection conn =JDBCUtils.getconnection();
            //2.3 创建预处理命令对象
            String sql="delete from tb_major where id=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            //2.4 执行SQL语句
            ps.setString(1,id);
            int rows=ps.executeUpdate();
            //2.5 释放资源
            ps.close();	conn.close();
            return rows;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;

    }

}
